package com.qdaily.ui.controller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.qdaily.ui.MainActivity;
import com.qdaily.ui.data.Constants;
import com.qdaily.ui.net.QDEnum;
import com.qdaily.ui.net.model.ArticleMeta;
import com.qdaily.ui.net.model.ArticleMetaCategory;
import com.qdaily.ui.net.model.ArticleMetaTag;
import com.qdaily.ui.ui.ArticleDetailsActivity;
import com.qdaily.ui.ui.CategoryActivity;
import com.qdaily.ui.ui.VotingDetailsActivity;
import com.qdaily.ui.ui.VotingMobDetailsActivity;
import com.qdaily.ui.ui.jpush.JpushObj;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by song on 15/5/8.
 */
public class ActivityController {

    //推送传给主页的值。
    public static final String PUSH_NOTIFICATION_DATA = "push_notification_data";

    private List<Activity> activityList = new LinkedList<Activity>();
    private static ActivityController instance;

    private ActivityController() {

    }

    // 单例模式中获取唯一的ExitApplication 实例
    public static ActivityController getInstance() {
        if (null == instance) {
            instance = new ActivityController();
        }
        return instance;
    }

    // 添加Activity 到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public void removeActivity(Activity activity) {
        activityList.remove(activity);
    }


    // 遍历所有Activity 并finish

    public boolean isExist(String activityName) {
        if (activityList != null && activityList.size() > 0) {
            for (Activity activity : activityList) {
                if (activity.getClass().getName().equals(activityName))
                    return true;
            }
        }
        return false;
    }

    public void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }

        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public void closeApp() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    public static void DisplayDetailActivity(Context mContext, ArticleMeta articleMeta) {
        if (articleMeta != null && articleMeta.getPost() != null) {
            if (Constants.IGenreType.isArticle(articleMeta.getPost().getGenre())) {
                DisplayArticle(mContext, articleMeta);
            } else if (Constants.IGenreType.isMob(articleMeta.getPost().getGenre())) {
                DisplayMob(mContext, articleMeta);
            } else if (Constants.IGenreType.isVote(articleMeta.getPost().getGenre())) {
                DisplayVote(mContext, articleMeta);
            }
        }
    }

    public static void DisplayArticle(Context mContext, ArticleMeta articleMeta) {
        Intent intent = new Intent(mContext, ArticleDetailsActivity.class);
        intent.putExtra(ArticleDetailsActivity.KEY_DISPLAY_ARTICLE_DETAILS, articleMeta);
        mContext.startActivity(intent);
    }

    public static void DisplayArticle(Context mContext, int articleId) {
        Intent intent = new Intent(mContext, ArticleDetailsActivity.class);
        intent.putExtra(ArticleDetailsActivity.KEY_DISPLAY_ARTICLE_ID, articleId);
        mContext.startActivity(intent);
    }

    public static void DisplayVote(Context mContext, int voteId) {
        Intent intent = new Intent(mContext, VotingDetailsActivity.class);
        intent.putExtra(VotingDetailsActivity.KEY_REQUEST_VOTE_ID, voteId);
        mContext.startActivity(intent);
    }

    public static void DisplayUserVote(Context mContext, int voteId) {
        Intent intent = new Intent(mContext, VotingDetailsActivity.class);
        intent.putExtra(VotingDetailsActivity.KEY_REQUEST_VOTE_ID, voteId);
        intent.putExtra(VotingDetailsActivity.KEY_REQUEST_FROMUSER, true);
        mContext.startActivity(intent);
    }


    public static void DisplayVote(Context mContext, ArticleMeta articleMeta) {
        Intent intent = new Intent(mContext, VotingDetailsActivity.class);
        intent.putExtra(VotingDetailsActivity.KEY_REQUEST_VOTING_DETAILS, articleMeta);
        mContext.startActivity(intent);
    }

    public static void DisplayMob(Context mContext, int mobId) {
        Intent intent = new Intent(mContext, VotingMobDetailsActivity.class);
        intent.putExtra(VotingMobDetailsActivity.KEY_REQUEST_MOB_ID, mobId);
        mContext.startActivity(intent);
    }

    public static void DisplayMob(Context mContext, ArticleMeta articleMeta) {
        Intent intent = new Intent(mContext, VotingMobDetailsActivity.class);
        intent.putExtra(VotingMobDetailsActivity.KEY_REQUEST_VOTING_DETAILS, articleMeta);
        mContext.startActivity(intent);
    }

    public static void DisplayCategory(Context mContext, ArticleMetaCategory category) {
        Intent intent = new Intent(mContext, CategoryActivity.class);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TYPE, QDEnum.CategoryFragmentEnum.CATEGORY);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_ID, category.getId());
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TITLE, category.getTitle());
        mContext.startActivity(intent);
    }

    public static void DisplayBestDesignCategory(Context mContext, ArticleMetaCategory category) {
        Intent intent = new Intent(mContext, CategoryActivity.class);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TYPE, QDEnum.CategoryFragmentEnum.BESTDESIGN);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_ID, category.getId());
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TITLE, category.getTitle());
        mContext.startActivity(intent);
    }

    public static void DisplayTag(Context mContext, ArticleMetaTag tag) {
        Intent intent = new Intent(mContext, CategoryActivity.class);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TYPE, QDEnum.CategoryFragmentEnum.TAG);
        intent.putExtra(CategoryActivity.KEY_CATEGORY_ID, tag.getId());
        intent.putExtra(CategoryActivity.KEY_CATEGORY_TITLE, tag.getTitle());
        mContext.startActivity(intent);
    }

    public static void DisplayDetailFromPush(Context mContext, JpushObj jpushObj) {

//        Intent intent = null;
//        switch (jpushObj.getGenre()) {
//            case 1:
//                intent = new Intent(mContext, ArticleDetailsActivity.class);
//                intent.putExtra(ArticleDetailsActivity.KEY_DISPLAY_ARTICLE_ID, jpushObj.getId());
//                break;
//            case 2:
//                break;
//            case 3:
//                intent = new Intent(mContext, VotingDetailsActivity.class);
//                intent.putExtra(VotingDetailsActivity.KEY_REQUEST_VOTE_ID, jpushObj.getId());
//                break;
//            case 4:
//                intent = new Intent(mContext, VotingMobDetailsActivity.class);
//                intent.putExtra(VotingMobDetailsActivity.KEY_REQUEST_MOB_ID, jpushObj.getId());
//                break;
//        }
//        if (intent != null) {
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
//                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
//                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            mContext.startActivity(intent);
//        }

        Intent intent = new Intent(mContext, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(PUSH_NOTIFICATION_DATA, jpushObj);
        mContext.startActivity(intent);
    }

    /**
     * 主页接收，分发跳转各个详情界面。
     *
     * @param mContext
     * @param intent
     */
    public void displayJpushContent(Context mContext, Intent intent) {
        JpushObj jpushObj = intent.getParcelableExtra(PUSH_NOTIFICATION_DATA);
        if (null == jpushObj) {
            try {
                throw new Exception("jpush data is broken , please check again");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        Intent detailIntnt = new Intent();
        switch (jpushObj.getGenre()) {
            case 1:
                detailIntnt = new Intent(mContext, ArticleDetailsActivity.class);
                detailIntnt.putExtra(ArticleDetailsActivity.KEY_DISPLAY_ARTICLE_ID, jpushObj.getId());
                break;
            case 3:
                break;
            case 2:
                detailIntnt = new Intent(mContext, VotingDetailsActivity.class);
                detailIntnt.putExtra(VotingDetailsActivity.KEY_REQUEST_VOTE_ID, jpushObj.getId());
                break;
            case 4:
                detailIntnt = new Intent(mContext, VotingMobDetailsActivity.class);
                detailIntnt.putExtra(VotingMobDetailsActivity.KEY_REQUEST_MOB_ID, jpushObj.getId());
                break;
        }
        mContext.startActivity(detailIntnt);
    }

}
